﻿using System;
using System.Web.UI.WebControls;

using Sunyi.Project.WebPoint.BusinessLogic;
using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataContract;
using Sunyi.Project.WebPoint.Resource;
using Sunyi.Project.WebPoint.WebControl;

namespace ActivitySystem.Web.Admin
{
    public partial class PageEdit : WebPointPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    Guid pageId = new Guid(Request.Params[WebPointGlobalConstant.PageId]);
                    BindPage(pageId);
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(this, ex);
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Page page = new Page();
            page.Id = new Guid(tbxId.Text);
            page.PageName = tbxPageName.Text;
            page.Url = tbxUrl.Text;
            page.Category = tbxCategory.Text;
            page.IsActive = Boolean.Parse(ddlStatus.SelectedValue);
            page.NavigationType = ddlNavigationType.SelectedValue;

            try
            {
                page.NavigationOrder = Int32.Parse(tbxNavigationOrder.Text);
            }
            catch
            {
                page.NavigationOrder = 0;
            }

            try
            {
                PageManager pageManager = new PageManager();
                pageManager.Update(page);

                AuditLogger.Write(AuditLogOperationCode.Update, this.GetType().Name, page);

                this.ShowClientMessage(WebResource.OP_UpdatePageSuccessful);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        #region Private Method

        private void BindPage(Guid pageId)
        {
            PageManager pageManager = new PageManager();
            Page page = pageManager.Select(pageId);

            if (page != null)
            {
                tbxId.Text = page.Id.ToString();
                tbxPageName.Text = page.PageName;
                tbxUrl.Text = page.Url;
                tbxCategory.Text = page.Category;
                tbxNavigationOrder.Text = page.NavigationOrder.ToString();
                WebPointWebCommon.SetItemSelected(ddlStatus, page.IsActive.ToString());

                foreach (ListItem item in ddlNavigationType.Items)
                {
                    item.Selected = item.Value == page.NavigationType;
                    if (item.Selected) break;
                }
            }
        }

        #endregion

        #region WebPointPage Member

        protected override void ApplyPermission()
        {
            if (!this.HasPermission(PermissionType.Update))
            {
                btnSubmit.Visible = false;
            }

            if (this.RequestPermissionType == PermissionType.Read)
            {
                btnSubmit.Visible = false;
            }
        }

        #endregion
    }
}